package com.itsu.mysql.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itsu.interfaces.entity.UserEntity;
import com.itsu.interfaces.entity.UserLogin;
import com.itsu.interfaces.service.UserService;
import com.itsu.mysql.mappers.UserMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserEntity findByMobile(String mobile) {
        return userMapper.selectOne(Wrappers.lambdaQuery(UserEntity.class).eq(UserEntity::getMobile, mobile));
    }

    @Override
    public UserEntity addUser(UserEntity userEntity) {
        userMapper.insert(userEntity);
        return userEntity;
    }

    @Override
    public UserEntity findUserById(Long uid) {
        return userMapper.selectOne(Wrappers.lambdaQuery(UserEntity.class).eq(UserEntity::getId, uid));
    }

    @Override
    public void update(UserEntity entity) {
        userMapper.update(entity, Wrappers.lambdaUpdate(UserEntity.class).eq(UserEntity::getId, entity.getId()));
    }

    @Override
    public int allCount() {
        return userMapper.selectList(null).size();
    }

    @Override
    public int registerUser(Date start, Date end) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        return userMapper.selectCount(queryWrapper.between("created", start, end)
        );
    }

    @Override
    public List<UserEntity> registerUserList(Date start, Date end) {
        return userMapper.selectList(Wrappers.lambdaQuery(UserEntity.class).between(UserEntity::getCreated, start, end).orderByAsc(UserEntity::getCreated));
    }

}
